﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using PasswordProvider.ObjectModel;
using PasswordProvider.ViewModel.Properties;
using PasswordProvider.ViewModel.Service;
using PasswordProvider.ViewModel.Helpers;
using PasswordProvider.ViewModel.Framework;
using System.ComponentModel;

namespace PasswordProvider.ViewModel
{
    internal sealed class LogOnSiteListItemViewModel : SiteListItemViewModel
    {
        public LogOnSiteListItemViewModel(LogOnSite site) 
            : base(site)
        {
            Settings.Default.PropertyChanged += new PropertyChangedEventHandler(ViewSettings_PropertyChanged);
        }

        #region Properties

        /// <summary>
        /// Icon url, used in binding by SitesListView
        /// </summary>
        public string IconUrl
        {
            get
            {
                if (Settings.Default.ShowSiteIcons)
                {
                    LogOnSite logOnSite = (LogOnSite)Site;
                    // Not every web site has an explicit icon url on the web home page, 
                    // it is usually possible to find it dynamically, using this helper. 
                    return FaveIconUrlHelper.GetFaveIconUrl(logOnSite.Url, logOnSite.IconUrl);
                }
                return null; // return null if not showing icons.
            }
        } 


        #endregion

        #region Overrides

        protected override void UpdateDisplay()
        {
            base.UpdateDisplay();
            OnPropertyChanged("IconUrl");
        }

        public override string GroupDisplayName
        {
            get { return Strings.ListItem_LoginSite_DisplayName; }
        }
        protected override INotifyChangesCommited GoToSiteDetails()
        {
            return (INotifyChangesCommited)NavigationManager.GoToSiteDetailsView(Site as LogOnSite, false);
        }
        protected override string GenerateDisplayName()
        {
            return CreateDisplayName(Site as LogOnSite);
        }
        protected override string GenerateToolTip()
        {
            return CreateToolTip(Site as LogOnSite);
        }

        protected override bool NavigateToWebSiteCommandCanExecute()
        {
            LogOnSite site = (LogOnSite)Site;
            return (site.Url != null);
        }

        protected override void NavigateToWebSiteCommandExecuted()
        {
            base.NavigateToWebSite(Site as LogOnSite);
        }

        #endregion

        #region Private Helper Methods

        /// <summary>
        /// Handle property changed. Virtual - but be careful to call base class implementation!
        /// </summary>
        private void ViewSettings_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "ShowSiteIcons":
                    OnPropertyChanged("IconUrl");
                    break;
            }
        }

        private static string CreateDisplayName(LogOnSite site)
        {
            StringBuilder name = new StringBuilder(site.Name);
            if (!String.IsNullOrEmpty(site.LogOnId))
            {
                name.Append(" (");
                name.Append(site.LogOnId);
                name.Append(")");
            }
            return name.ToString();
        }
        private static string CreateToolTip(LogOnSite site)
        {
            StringBuilder toolTip = new StringBuilder();
            if (site.Url != null)
            {
                toolTip.Append(site.Url.AbsoluteUri);
                toolTip.AppendLine();
            }
            if (!String.IsNullOrEmpty(site.Description))
            {
                toolTip.Append(site.Description);
                toolTip.AppendLine();
            }
            if (site.Tags != null && site.Tags.Count > 0)
            {
                toolTip.Append("Tags: ");
                foreach (string tag in site.Tags)
                {
                    toolTip.Append(tag);
                    if (tag != site.Tags.Last())
                    {
                        toolTip.Append(", ");
                    }
                }
                toolTip.AppendLine();
            }
            if (site.EncryptedPassword != null)
            {
                toolTip.AppendFormat("Last access date: {0}", site.EncryptedPassword.LastAccessDate);
                toolTip.AppendLine();
                toolTip.AppendFormat("Access count: {0}", site.EncryptedPassword.AccessCount);
            }

            return toolTip.ToString();

        }

        #endregion // Private Helper Methods


    }
}
